package com.cloud.neo4j.common.neo4j;

import com.cloud.neo4j.business.dto.result.ResultEntity;
import com.cloud.neo4j.business.neo4j.car.entity.business.PersonRelationShipEntity;
import org.neo4j.driver.Record;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Relationship;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 处理neo4j返回的数据集合
 * @author 刘房
 */
public class Neo4jRelationShipResponse {

    /**
     * 处理Neo4j返回的数据集合
     * @param list  Neo4j查询结果
     * @return  List<ResultEntity>
     */
    public static List<PersonRelationShipEntity> getResultEntity(List<Record> list){
        List<PersonRelationShipEntity> resultEntityList = new ArrayList<>();
        if(list.size() < 1) {
            return null;
        }
        /** 处理数据 */
        for (int i = 0; i < list.size(); i++) {
            Record record = list.get(i);
            PersonRelationShipEntity resultEntity = new PersonRelationShipEntity();
            resultEntity.setStartPerson(setNodeCov(record.get("p").asNode()));
            resultEntity.setNumber(record.get("number").asInt());
            resultEntity.setEndPerson(setNodeCov(record.get("pw").asNode()));
            resultEntityList.add(resultEntity);
        }
        return resultEntityList;
    }

    /**
     * Node转换
     * @param node  节点结果转换
     * @return  map
     */
    public static Map<String,Object> setNodeCov(Node node){
        Map<String, Object> nodeOne = new HashMap<>();
        nodeOne.putAll(node.asMap());
        nodeOne.put("id",node.id());
        nodeOne.put("labels",node.labels());
        return nodeOne;
    }
}
